''' 项目管理 '''
from pms import pmsservice as db
from flask import Blueprint, jsonify, redirect, url_for, request
import logging, requests
import sys, traceback, os, re ,io
import openpyxl
from flask import send_from_directory, make_response

logger = logging.getLogger('app')

pms = Blueprint("pms", __name__, url_prefix="/api")

#获取employee信息
@pms.route('/project/project_data',methods=['GET'])
def get_project_data():
    # 获取查询字符串参数，读取到的参数是字符串类型；用print(type(pageSize),type(pageCurrent),type(searchValue))验证
    logger.info("获取项目档案-项目列表页面数据")
    fordata = request.args.to_dict()
    result = db.get_project_data(fordata)
    return jsonify(result)

#删除项目
@pms.route('/project/del_project',methods=['POST'])
def del_project():
    formdata = request.json
    print("received data", formdata)
    result = db.del_project(formdata)
    return jsonify(result)

#创建项目
@pms.route('/project/add_project',methods=['GET','POST'])
def add_project():
    formdata = request.json
    logger.info(formdata)
    print(f"add project: {formdata}")
    result = db.add_project(formdata)
    return jsonify(result)
#更新项目信息页面初始化
@pms.route('/project/edit_project_init',methods=['GET','POST'])
def edit_project_init():
    args = request.args.to_dict()
    logger.info(args)
    print(args)
    result = db.edit_project_init(args)
    return jsonify(result)
#更新项目信息
@pms.route('/project/update_project',methods=['GET','POST'])
def update_project():
    formdata = request.json
    logger.info(formdata)
    result = db.update_project(formdata)
    return jsonify(result)

#获取班组信息
@pms.route('/group/group_data',methods=['GET'])
def get_group_data():
    param = {}
    param['pageSize'] = request.args.get('pageSize', default=10, type=int)
    param['pageCurrent']= request.args.get('page', default=1, type=int)
    param['selectProject'] = request.args.get('selectProject')
    result = db.get_group_data(param)
    return jsonify(result)

#获取选择项目下拉框选项
@pms.route('/group/projectOptions',methods=['GET'])
def get_projectOptions():
    result = db.get_projectOptions()
    return jsonify(result)

#获取选择班组下拉框选项
@pms.route('/group/groupOptions',methods=['GET'])
def get_groupOptions():
    result = db.get_groupOptions()
    return jsonify(result)

#创建项目班组
@pms.route('/group/add_group',methods=['GET','POST'])
def add_group():
    formdata = request.json
    logger.info(formdata)
    result = db.add_group(formdata)
    return jsonify(result)

#更新项目班组信息
@pms.route('/group/update_group',methods=['GET','POST'])
def update_group():
    formdata = request.json
    logger.info(formdata)
    result = db.update_group(formdata)
    return jsonify(result)

#删除班组信息
@pms.route('/group/batchDelGroup',methods=['POST'])
def batchDelGroup():
    formdata = request.json
    result = db.batchDelGroup(formdata)
    return jsonify(result)

#批量导入班组信息数据
@pms.route('/group/uploadgroupexcel',methods=['POST'])
def uploadgroupexcel():
    file = request.files.get('file')
    f = file.read()
    result = 'ok'
    try:
        # 打开工作薄
        excel_content = io.BytesIO(f)
        excel_file = openpyxl.load_workbook(excel_content)
        ws = excel_file['班组信息']
        db.import_group(ws)
        excel_file.close()
    except:
        result = 'ng'
        logger.error(sys.exc_info())
    finally:
        return jsonify(result)

#批量导出班组信息数据
@pms.route('/group/getgroupexcel',methods=['GET'])
def getgroupexcel():
    args = request.args.to_dict()
    
    # 获取模板文件路径
    dir_path = str(os.path.abspath(os.path.dirname(__file__)))
    dirpath = os.path.join(os.path.join(dir_path.replace('\pms',''), 'template'))
    
    result = db.getgroupexcel(dirpath, args)

    response = make_response(send_from_directory(dirpath, 'export班组信息.xlsx', as_attachment=True))
    response.headers["Access-Control-Expose-Headers"] = "Content-disposition"

    return response


